﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;
using Microsoft.Exchange.WebServices.Data;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Exchange;
using System.Threading;
using System.IO;
using System.Net;
using System.Collections.ObjectModel;
using System.DirectoryServices;
using System.Xml;
using System.DirectoryServices.ActiveDirectory;


namespace Main
{
    public static class ControlFunctions
    {

        public class MailClass
        {
            /// <summary>
            /// returns login address of the "info" account on SQL Azure to login to Exchange Online
            /// </summary>
            /// <returns></returns>
            private String getInfoAddress(string companyName)
            {
                TechnoStoreMainDataContext db = new TechnoStoreMainDataContext();
                var query = from u in db.Users
                            where u.name == "TECHNOSTORE" && u.surname == "INFO" && u.company == companyName
                            select u.email; 
                return query.SingleOrDefault();
            }

            /// <summary>
            /// returns login password of the "info" account on SQL Azure to login to Exchange Online
            /// </summary>
            /// <returns></returns>
            private String getInfoPassword(string companyName)
            {
                TechnoStoreMainDataContext db = new TechnoStoreMainDataContext();
                var query = from u in db.Users
                            where u.name == "TECHNOSTORE" && u.surname == "INFO" && u.company == companyName
                            select u.password; 
                return query.SingleOrDefault();
            }

            string _address;
            string _subject;
            string _body;
            string _companyName;

            public MailClass(string address, string subject, string body, string companyName)
            {
                _address = address;
                _subject = subject;
                _body = body;
                _companyName = companyName;
            }

            static bool RedirectionUrlValidationCallback(String redirectionUrl)
            {
                // Perform validation.
                // Validation is developer dependent to ensure a safe redirect.
                return true;
            }

            public void SendMail()
            {
                ExchangeService ews = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
                ews.Credentials = new WebCredentials(getInfoAddress(_companyName), getInfoPassword(_companyName));
                ews.AutodiscoverUrl(getInfoAddress(_companyName), RedirectionUrlValidationCallback);
                
                EmailMessage msg = new EmailMessage(ews);
                msg.Subject = _subject;
                msg.Body = _body;
                msg.ToRecipients.Add(_address);
                msg.SendAndSaveCopy();
            }
        }

        /// <summary>
        /// send an e-mail notification with username and password to newly created user via Exchnage Online
        /// </summary>
        /// <param name="address"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <returns></returns>
        public static bool SendEMail(string address, string subject, string body, string companyName)
        {
            try
            {
                MailClass mail = new MailClass(address, subject, body, companyName);
                Thread thread = new Thread(mail.SendMail);
                thread.Start();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
    }
}